package is.common.util;

import is.common.datatype.Request;
import org.w3c.dom.Document;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author aspr0
 *         Date: 02.05.12
 *         Time: 5:35
 */
public class ObjectStreamer
{
    private ObjectInputStream objectInputStream;
    private ObjectOutputStream objectOutputStream;


    public ObjectStreamer(ObjectInputStream objectInputStream, ObjectOutputStream objectOutputStream)
    {
        this.objectInputStream = objectInputStream;
        this.objectOutputStream = objectOutputStream;
    }


    /* receivers - no cast more! */
    public String receiveString() throws IOException, ClassNotFoundException
    {
        return (String) receiveObject();
    }

    public String[] receiveStrings() throws IOException, ClassNotFoundException
    {
        return (String[]) receiveObject();
    }

    public String[][] receiveStringsDouble() throws IOException, ClassNotFoundException
    {
        return (String[][]) receiveObject();
    }

    public Request receiveRequest() throws IOException, ClassNotFoundException
    {
        return (Request) receiveObject();
    }

    public Document receiveDoc() throws IOException, ClassNotFoundException
    {
        return (Document) receiveObject();
    }


    public void transmitObject(Object object) throws IOException
    {
        objectOutputStream.writeObject(object);
    }


    private Object receiveObject() throws ClassNotFoundException, IOException
    {
        return objectInputStream.readObject();
    }
}
